<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>文章分类</span>
        <el-button type="primary" @click="addDialog = true">添加分类</el-button>
      </div>
      <!-- 内容区 -->
      <el-table :data="list" border style="width: 100%" stripe>
        <el-table-column fixed prop="id" label="序号" width="150"> </el-table-column>
        <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template v-slot="scope">
            <el-button size="small" @click="del(scope.row.id)">删除</el-button>
            <el-button size="small" @click="showEditDialog(scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加的对话框 -->
    <el-dialog :before-close="cancel" title="添加类别" :visible.sync="addDialog" width="500px">
      <el-form label-width="80px" :model="addData" :rules="rules" ref="addForm">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addData.cate_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addData.cate_alias" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 修改的对话框 -->
    <el-dialog :before-close="editCancel" title="修改类别" :visible.sync="editDialog" width="500px">
      <el-form label-width="80px" ref="editForm" :model="editData" :rules="rules">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editData.cate_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editData.cate_alias" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editCancel">取 消</el-button>
        <el-button type="primary" @click="updateCate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCateListAPI, addCateAPI, delCateAPI, updateCateAPI } from '@/api/category'
export default {
  data() {
    return {
      list: [],
      // 控制添加弹框数据项
      // false表示隐藏对话框，true表示显示
      addDialog: false,
      editDialog: false,
      // 收集添加表单的数据
      addData: {
        cate_name: '',
        cate_alias: ''
      },
      editData: {
        cate_name: '',
        cate_alias: '',
        id: ''
      },
      // 验证规则
      rules: {
        cate_name: [
          { required: true, message: '分类名称必填', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在1~10位', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '分类别名必填', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '长度1~15位的数字字母组合', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 获取分类列表数据
    async getCateList() {
      const { data: res } = await getCateListAPI()
      if (res.code === 0) {
        this.list = res.data
      }
    },
    // 添加分类的方法
    addCate() {
      this.$refs.addForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await addCateAPI(this.addData)
        if (res.code === 0) {
          this.$message.success(res.message)
          this.$refs.addForm.resetFields()
          this.getCateList()
          this.addDialog = false
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 取消添加的方法
    cancel() {
      this.$refs.addForm.resetFields()
      this.addDialog = false
    },
    del(id) {
      this.$confirm('确认要删除吗?', '提示', {
        type: 'warning',
        callback: async r => {
          if (r === 'confirm') {
            // 发送Ajax请求
            const { data: res } = await delCateAPI(id)
            if (res.code === 0) {
              this.$message.success(res.message)
              this.getCateList() // 重新获取数据渲染页面
            } else {
              this.$message.error(res.message)
            }
          }
        }
      })
    },
    // 点击修改，数据回填
    showEditDialog(row) {
      this.editData = { ...row }
      this.editDialog = true
    },
    // 修改分类
    updateCate() {
      this.$refs.editForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await updateCateAPI(this.editData)
        if (res.code === 0) {
          this.$message.success(res.message)
          this.getCateList()
          this.editDialog = false
          this.$refs.editForm.resetFields()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 取消修改
    editCancel() {
      this.$refs.editForm.resetFields()
      this.editDialog = false
    }
  },
  created() {
    this.getCateList()
  }
}
</script>

<style lang="less" scoped>
.clearfix {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
